/*
 * @Description: node实现加密解密 
 * @Version: 1.0
 * @Autor: ziwei
 * @Date: 2021-09-17 16:03:11
 * @LastEditors: ziwei
 * @LastEditTime: 2021-09-17 16:25:22
 */
let {generateKeyPairSync,privateEncrypt,publicDecrypt} = require('crypto');
//生成一堆密钥对，一个是公钥，一个是私钥
let rsa = generateKeyPairSync('rsa',{
  modulusLength:1024,
  publicKeyEncoding:{
    type:'spki',
    format:'pem',//base64格式的私钥
  },
  privateKeyEncoding:{
    type:'pkcs8',//公钥
    format:'pem',
    cipher:'aes-256-cbc',
    passphrase:'passphrase'
  }
});
let message = 'hello';
let encryptMessage = privateEncrypt({
  key:rsa.privateKey,passphrase:'passphrase'
},Buffer.from(message,'utf8'))
console.log(encryptMessage)
let decryptMessage = publicDecrypt(rsa.publicKey,encryptMessage);
console.log("decryptMessage",decryptMessage.toString());